﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using DlhSoft.Windows.Data;
using DlhSoft.Windows.Controls;
using UTC_Project_Manager.Model.Infrastructure;

namespace UTC_Project_Manager.Controls
{
    /// <summary>
    /// Interaction logic for DescriptionControl.xaml
    /// </summary>
    public partial class DescriptionControl : UserControl
    {
        private int taskIndex;

        public DescriptionControl()
        {
            InitializeComponent();
            // Populate MemberComboBox
            this.attributionComboBox.ItemsSource = App.MyConfig.assignableResourceItems;
            // Listen the event in GanttControl
            GanttControl.descriptionValuesEvent += new GanttControl.PassDescriptionValues(descriptionValuesHandlerMethod);
            GanttControl.cleanDescriptionValuesEvent += new GanttControl.CleanDescriptionValues(cleanControl);
        }

        // Handle the description event
        public void descriptionValuesHandlerMethod(Hashtable descValues)
        {
            //cleanControl();
            String taskName = descValues["TaskName"].ToString();
            String taskIndexString = descValues["TaskIndexString"].ToString();
            String memberName = descValues["MemberName"].ToString();
            this.taskIndex = Convert.ToInt32(taskIndexString);
            int i = this.taskIndex - 1;

            this.nameTextBox.Text = taskName;
            if (App.MyConfig.OurProject.Tasks.Count > 0)
            {
                if (App.MyConfig.OurProject.Tasks.ElementAt(i).Description != null)
                    this.descriptionTextBox.Text = App.MyConfig.OurProject.Tasks.ElementAt(i).Description;
                else this.descriptionTextBox.Text = "";
                if (App.MyConfig.OurProject.Tasks.ElementAt(i).Note != null)
                    this.notesTextBox.Text = App.MyConfig.OurProject.Tasks.ElementAt(i).Note;
                else this.notesTextBox.Text = "";
            }

            int index = 0;
            foreach (ResourceItem item in this.attributionComboBox.Items)
            {
                // No member assigned
                if ( memberName == "None")
                {
                    index = -1; // Reset the index
                    break;
                }
                // Member found
                else if (item.Content.ToString() == memberName) break;
                index++;
            }
            // Highlight current member
            this.attributionComboBox.SelectedIndex = index;
            // Activates buttons
            this.okButton.IsEnabled = true;
            this.cancelButton.IsEnabled = true;
            this.deleteButton.IsEnabled = true;
            // Event handler for delete button
            this.deleteButton.Command = ApplicationCommands.Delete;
            this.deleteButton.CommandTarget = descValues["TaskListView"] as TaskListView;
        }

        // Reset controls.
        private void cleanControl() {
            this.nameTextBox.Text = "";
            this.notesTextBox.Text = "";
            this.descriptionTextBox.Text = "";
            this.attributionComboBox.SelectedIndex = -1;
        }

        private void disableButtons() {
            // Disable buttons
            this.okButton.IsEnabled = false;
            this.cancelButton.IsEnabled = false;
            this.deleteButton.IsEnabled = false;
        }

        // Delete task
        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            cleanControl();
            disableButtons();
        }

        // Fix description and notes when a "new task" is added
        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            int index = 0;
            if (this.taskIndex >= 0) index = this.taskIndex - 1;
            // Search task in ProjectTask List
            int i = getTaskIndex();
            // Update Task description and note
            App.MyConfig.OurProject.Tasks.ElementAt(i).Description = this.descriptionTextBox.Text;
            App.MyConfig.OurProject.Tasks.ElementAt(i).Note = this.notesTextBox.Text;

            App.MyConfig.taskItems.ElementAt(index).Content = this.nameTextBox.Text;
            if (this.attributionComboBox.SelectedValue != null)
                App.MyConfig.taskItems.ElementAt(index).AssignmentsString = this.attributionComboBox.SelectedValue.ToString();
        }

        public int getTaskIndex() {
            int i = 0;
            foreach (ProjectTask pTask in App.MyConfig.OurProject.Tasks)
            {
                if (pTask.IndexString == this.taskIndex.ToString()) break;
                i++;
            }
            return i;
        }
    }
}
